﻿using Newtonsoft.Json.Serialization;
using System.Collections;

namespace StarsAdmin.Core.Extensions.Json
{
    public class CollectionValueProperty : IValueProvider
    {
        private readonly IValueProvider _valueProvider;

        public CollectionValueProperty(IValueProvider valueProvider)
        {
            _valueProvider = valueProvider;
        }

        public object GetValue(object target)
        {
            var value = _valueProvider.GetValue(target);
            if (value is ICollection<object>)
            {
                // 如果集合为空，则返回null
                var collection = value as ICollection;
                return collection?.Count > 0 ? value : null;
            }
            return value;
        }

        public void SetValue(object target, object value)
        {
            _valueProvider.SetValue(target, value);
        }
    }
}